package com.zxl.live.screen.ui.receiver;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

/**
 * Created by Administrator on 2018/2/6.
 */

public class ReferrerManager {

    private static ReferrerManager sInstance;
    private Context mContext;
    private SharedPreferences mSp;

    public static ReferrerManager getInstance(Context context) {
        if (sInstance == null) {
            synchronized (ReferrerManager.class) {
                if (sInstance == null) {
                    sInstance = new ReferrerManager(context);
                }
            }
        }
        return sInstance;
    }

    private ReferrerManager(Context context) {
        this.mContext = context != null ? context.getApplicationContext() : null;
        this.mSp = mContext.getSharedPreferences("commerce_buy_channel", 0);
    }

    public boolean checkFirstReceiver() {
        return this.mSp.getBoolean("isfirst", true);
    }

    public void saveFirstReceiver() {
        this.mSp.edit().putBoolean("isfirst", false).commit();
    }

    public void saveReferrer(String referrer) {
        if (!TextUtils.isEmpty(referrer)) {
            referrer = referrer.replaceAll("\\n", "");
            referrer = referrer.replaceAll("\\u007C", "#");
        }
        this.mSp.edit().putString("referrer", referrer).commit();
    }

    public String getReferrer() {
        String oldReferrer = this.mSp.getString("referrer", null);
        return oldReferrer;
    }

    public String getCampaign(String referrer) {
        if (!TextUtils.isEmpty(referrer)) {
            String[] splits = referrer.split("&");
            if (splits != null && splits.length >= 0) {
                String[] var3 = splits;
                int var4 = splits.length;

                for (int var5 = 0; var5 < var4; ++var5) {
                    String string = var3[var5];
                    if (string != null && string.toLowerCase().contains("campaign")) {
                        String[] utmSources = string.split("=");
                        if (utmSources != null && utmSources.length > 1) {
                            return utmSources[1];
                        }
                    }
                }
            }
        }
        return null;
    }

    public String getCampaignId(String referrer) {
        if (!TextUtils.isEmpty(referrer)) {
            String[] splits = referrer.split("&");
            if (splits != null && splits.length >= 0) {
                String[] var3 = splits;
                int var4 = splits.length;
                for (int var5 = 0; var5 < var4; ++var5) {
                    String string = var3[var5];
                    if (string != null && string.toLowerCase().contains("campaignid")) {
                        String[] utmSources = string.split("=");
                        if (utmSources != null && utmSources.length > 1) {
                            return utmSources[1];
                        }
                    }
                }
            }
        }
        return null;
    }

    public String getUtmSource(String referrer) {
        if (!TextUtils.isEmpty(referrer)) {
            String[] splits = referrer.split("&");
            if (splits != null && splits.length >= 0) {
                String[] var3 = splits;
                int var4 = splits.length;

                for (int var5 = 0; var5 < var4; ++var5) {
                    String string = var3[var5];
                    if (string != null && string.contains("utm_source")) {
                        String[] utmSources = string.split("=");
                        if (utmSources != null && utmSources.length > 1) {
                            return utmSources[1];
                        }
                    }
                }
            }
        }
        return null;
    }
}
